package com.woniu.liuzhougov.relatedinformation.controller;

import com.woniu.liuzhougov.businessproject.pojo.BusinessProject;
import com.woniu.liuzhougov.businessproject.vo.Result;
import com.woniu.liuzhougov.fileManage.constant.SysConstant;
import com.woniu.liuzhougov.fileManage.pojo.FileData;
import com.woniu.liuzhougov.fileManage.service.FileService;
import com.woniu.liuzhougov.relatedinformation.pojo.City;
import com.woniu.liuzhougov.relatedinformation.pojo.RelatedInformation;
import com.woniu.liuzhougov.relatedinformation.service.RelatedInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Description: desc
 * @author: Chris
 * @createDate: 2019/4/24 0024
 * @version: 1.0
 */
public abstract class AbstractRelatedInformationController<T> {

    @Autowired
    private FileService fileService;

    @Value("${upload.path}")
    private String filePath;

    public Result parentList(RelatedInformationService<T> relatedInformationService){
        return new Result(relatedInformationService.selectList());
    }

    public Result parentSearch(RelatedInformationService<T> relatedInformationService,String name){
        return new Result(relatedInformationService.selectByName(name));
    }

    public Result parentDelete(RelatedInformationService<T> relatedInformationService,Integer[] deletelist){
        int i = 0;
        for (Integer id:deletelist) {
            if ( relatedInformationService.deleteById(id)>0){
                i++;
            }
        }
        return new Result(code(i));
    }

    public Result parentAdd(RelatedInformationService<T> relatedInformationService,T t){
        return new Result(code(relatedInformationService.insert(t)));
    }

    public Result parentUpdate(RelatedInformationService<T> relatedInformationService, T t){
        return new Result(code(relatedInformationService.update(t)));
    }

    public Result parentInitForm(RelatedInformationService<T> relatedInformationService, Integer id){
        return new Result(relatedInformationService.selectById(id));
    }

    public String code(int code){
        if (code > 0){
            return "200";
        }else {
            return "500";
        }
    }

    public com.woniu.liuzhougov.common.utils.Result parentAddFile(RelatedInformation fileData){
        SysConstant.setRelatedInformationFileDataId(fileData);
        if (SysConstant.code == 200){
            return com.woniu.liuzhougov.common.utils.Result.success(200,"成功");
        }else {
            return com.woniu.liuzhougov.common.utils.Result.error(500,"失败");
        }
    }

    public com.woniu.liuzhougov.common.utils.Result parentUpload(RelatedInformationService relatedInformationService,MultipartFile file, HttpServletRequest request) throws IOException,IllegalStateException {
        //文件名uuid
        String filename = file.getOriginalFilename();
        String filenameUUID = UUID.randomUUID().toString()+","+filename;

        FileData fileData = SysConstant.getFileData();

        Boolean SaveStatus = saveToLocal(file,filenameUUID);

        Boolean DBstatus = saveToDB(fileData,file,filenameUUID);

        Boolean BusinessUpdateFile = updateFileID(relatedInformationService,filenameUUID);

        if (SaveStatus && DBstatus && BusinessUpdateFile){
            SysConstant.code = 200;
            return com.woniu.liuzhougov.common.utils.Result.success(200,"成功");
        }else {
            SysConstant.code = 500;
            return  com.woniu.liuzhougov.common.utils.Result.error(500,"失败");
        }
    }

    public Boolean updateFileID(RelatedInformationService relatedInformationService,String filenameUUID) {
        FileData uploadFile = fileService.selectFileByName(filenameUUID);
        RelatedInformation relatedInformation = (RelatedInformation) relatedInformationService.selectById(SysConstant.fileDataId);
        if (uploadFile!=null){
            if ("".equals(relatedInformation.getFileId())||relatedInformation.getFileId()==null){
                relatedInformation.setFileId(String.valueOf(uploadFile.getFileId()));
            }else{
                relatedInformation.setFileId(relatedInformation.getFileId()+","+ uploadFile.getFileId());
            }
        }

        int i = relatedInformationService.update(relatedInformation);
        if (i > 0){
            return true;
        }else{
            return false;
        }
    }

    public Boolean saveToDB(FileData fileData, MultipartFile file,String filenameUUID) {
        fileData.setFileName(filenameUUID);
        fileData.setFileUrl(filePath);
        fileData.setFileSize(String.valueOf(file.getSize()));
        fileData.setFileLoaderId(1);    //登录用户id
        fileData.setItemId(9);
        if (fileService.uploadFile(fileData)>0){
            return true;
        }
        return false;
    }

    public Boolean saveToLocal(MultipartFile file,String filenameUUID) {
        String filename =file.getOriginalFilename();
        String path = filePath;
        //文件夹判断
        File filepackage = new File(path);
        if (!filepackage.exists()){
            filepackage.mkdirs();
        }
        //文件防重名 File.separator用于区分windows下的/
        path = path+File.separator+filenameUUID;
        return saveFile(filepackage,path,file);
    }

    public Boolean saveFile(File filepackage, String path, MultipartFile file) {
        filepackage = new File(path);
        try {
            file.transferTo(filepackage);   //写入数据
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            SysConstant.isInsert = false;
            return  false;
        }
    }

}
